package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.dao.BannerDao;
import com.baizhi.cmfz.entity.Banner;
import com.baizhi.cmfz.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class BannerServiceImpl implements BannerService {
    @Autowired(required = false)
    private BannerDao bannerDao;

    /**
     * 首页分页展示轮播图的service方法
     * layui页面传递来两个参数，page，limit
     * 需要向控制器返回一个map map内是固定的key 由框架定义好的键
     * @param page
     * @param limit
     * @return
     */
    @Override
    @Transactional
    public Map selectBanner(int page, int limit) {
        int offset = (page-1)*limit;
        List<Banner> banners = bannerDao.selectBanner(offset, limit);
        System.out.println("展示轮播图收参"+banners);
        //查询出总条数
        int count = bannerDao.selectCoent();
        Map map = new HashMap(4);
        map.put("code",0);
        map.put("msg","");
        map.put("data",banners);
        map.put("count",count);
        return map;
    }

    @Override
    @Transactional
    public Map addBanner(Banner banner) {
        System.out.println("添加轮播图收参"+banner);
        banner.setBannerDate(new Date());
        int i = bannerDao.addBanner(banner);
        Map map = new HashMap(2);
        if (i == 1) {
            map.put("isAdd",true);
        }else {
            map.put("isAdd",false);
        }
        return map;
    }

    @Override
    @Transactional
    public Map deleteBanner(int bannerId) {
        int i = bannerDao.deleteBanner(bannerId);
        Map map = new HashMap(2);
        if (i == 1) {
            map.put("isDelete",true);
        }else {
            map.put("isDelete",false);
        }
        return map;
    }

    @Override
    @Transactional
    public Banner selectOne(int bannerId) {
        Banner banner = bannerDao.selectOne(bannerId);
        return banner;
    }

    @Override
    public Map updateBanner(Banner banner) {
        int i = bannerDao.updateBanner(banner);
        Map map = new HashMap(2);
        if (i == 1) {
            map.put("isUpdate",true);
        }else {
            map.put("isUpdate",false);
        }
        return map;
    }

    @Override
    @Transactional
    public List<Banner> selectBannerByCount(int count) {
        return bannerDao.selectBannerByCount(count);
    }
}
